var util = {};

//时间格式化 毫秒转为 xxxx-xx-xx
//arg=2  毫秒转为 xxxx-xx-xx hh:mm
//arg=3  毫秒转为 xxxx-xx-xx hh:mm:ss
function formatTime(str, arg) {
  var unixTimestamp = new Date();
  unixTimestamp.setTime(str);
  var y = unixTimestamp.getFullYear();
  var M = unixTimestamp.getMonth();
  var d = unixTimestamp.getDate();
  var hh = unixTimestamp.getHours();
  var mm = unixTimestamp.getMinutes();
  var ss = unixTimestamp.getSeconds();
  M = Number(M) + 1 < 10 ? '0' + (Number(M) + 1) : Number(M) + 1;
  d = Number(d) < 10 ? '0' + d : Number(d);
  hh = Number(hh) < 10 ? '0' + hh : Number(hh);
  mm = Number(mm) < 10 ? '0' + mm : Number(mm);
  ss = Number(ss) < 10 ? '0' + ss : Number(ss);
  if (arg == 2) {
    return y + "-" + M + "-" + d + " " + hh + ":" + mm;
  } else if (arg == 3) {
    return y + "-" + M + "-" + d + " " + hh + ":" + mm + ":" + ss;
  } else {
    return y + "-" + M + "-" + d;
  };
};

util.formatTime = formatTime;
module.exports = util;
